package com.tianya.portal.common.utils;

import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.*;

public class AsposeWordUtils {
    private static final Logger log = LogManager.getLogger(AsposeWordUtils.class);

    /**
     * word转pdf
     * @param inputStream
     * @return
     */
    public static InputStream getWordToPdf(InputStream inputStream) {
        try {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            Document document = new Document(inputStream);
            document.save(outputStream, SaveFormat.PDF);
            byte[] bytes = outputStream.toByteArray();
            InputStream stream = new ByteArrayInputStream(bytes);
            return stream;
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }

    public static InputStream getWordToPdf1(InputStream inputStream) {
        try {
            //OutputStream outputStream = new ByteArrayOutputStream();
            //Document document = new Document(inputStream);
            Document document = new Document("D:\\学习记录\\测试\\123.docx");
            PipedInputStream in = new PipedInputStream();
            PipedOutputStream out = new PipedOutputStream(in);
            Thread thread = new Thread(
                    new Runnable() {
                        public void run() {
                            try {
                                System.out.println("3333333333333333333333333333333");
                                document.save("D:\\学习记录\\测试\\123.pdf", SaveFormat.PDF);
                                System.out.println("55555555555555555555555");
                                document.save(out, SaveFormat.PDF);
                                System.out.println("6666666666666666666666666666666666");
                                out.flush();
                                out.close();
                                System.out.println("2222222222222222222222222222");
                                //Thread.currentThread().interrupt();
                                //Thread.currentThread().join();
                            } catch (Exception e) {
                                log.error(e.getMessage());
                            }
                        }
                    });
            thread.start();
            //判断线程是否执行完成
            while (thread.getState() != Thread.State.TERMINATED) {
                // 继续读取
            }
            return in;
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }
}
